package com.xinhua;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinhua.domian.User;
import com.xinhua.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserMapperTest2 {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        List<User> userList = userMapper.selectList(null);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testinster() {
        User user = new User();
        user.setUsername("caocao");
        user.setName("曹操");
        user.setPassword("1234");
        user.setAge(15);
        user.setEmail("test145872");
        int insert = this.userMapper.insert(user);
        System.out.println("insert =>" + insert);
        System.out.println(user.getId());
    }

    @Test
    public void testGetById() {
        User user = userMapper.selectById(1);
        System.out.println(user);
    }

    @Test
    public void testupdateById() {
        User user = new User();
        user.setId(7);
        user.setName("于金彪");
        user.setPassword("66666");
        int i = userMapper.updateById(user);
        System.out.println(i);
    }

    @Test
    public void testupdate() {
        User user = new User();
        user.setEmail("test12478");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("User_name", "caocao");
        int update = this.userMapper.update(user, queryWrapper);
        System.out.println(update);
    }

    @Test
    public void testdeleteById() {
        int i = this.userMapper.deleteById(7);
        System.out.println(i);
    }

    @Test
    public void testdeleteBatchIds() {
        //根据id批量删除
        int i = this.userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
        System.out.println(i);
    }


    @Test
    public void testSelectBatchIds() {
        //根据id批量查询
        List<User> i = this.userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        System.out.println(i);
    }

    @Test
    public void testSelectOne() {
        //根据条件查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("User_name", "lisi");
        User user = this.userMapper.selectOne(wrapper);
        System.out.println(user);

    }

//    @Test
//    public void testSelectCount() {
//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.gt("age", 20);//条件：年龄大于20
//        Integer integer = this.userMapper.selectCount(wrapper);
//        System.out.println(integer);
//    }

    @Test
    public void testSelectList() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("email", "itcast");
        List<User> users = this.userMapper.selectList(wrapper);
        for (User u : users) {
            System.out.println(u);
        }
    }

    @Test
    public void testSelectPage() {
        IPage<User> page = new Page<>(2, 5);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("email", "itcast");
        IPage<User> page1 = this.userMapper.selectPage(page, wrapper);
        System.out.println("数据总条数" + page1.getTotal());
        System.out.println("数据总页数" + page1.getPages());
        System.out.println("当前页数" + page1.getCurrent());
        System.out.println("每页显示条数" + page1.getSize());
        List<User> records = page1.getRecords();
        for (User r : records) {
            System.out.println(r);
        }
    }
}
